﻿using System;
using System.Collections.Generic;
using WAD.DAL;

namespace WAD.BLL
{
    public class FCSharedBLL
    {
        private static object lockObj = new object();
        public static string Shared(int id, int user_id, int money_id, string picurl, string DES_KEY)
        {
            bool isErr = false;
            int count1 = 0, count2 = 0, count3 = 0, count4 = 0;
            int article_id = 0;
            decimal price = 0, balance = 0;
            string msg = "";

            //获取分享记录
            Dictionary<string, object> model = DAOFCShared.Get("fc_shared", "id=" + id, "");

            if (model == null)
                return "找不到分享记录";

            if (DateTime.Now.Subtract(Convert.ToDateTime(model["date"])).TotalMinutes > 30)
                return "超过30分钟，红包失效";

            article_id = Convert.ToInt32(model["article_id"]);
           
            if (Convert.ToInt32(model["state"]) == 1)
            {
                DAOFCShared.Shared(id, picurl);
                return "";
            }

            //获取红包价格
            Dictionary<string, object> sh_money = DAOFCSharedMoney.Get(money_id);
            if (sh_money == null || Convert.ToInt32(sh_money["user_id"]) != user_id || Convert.ToInt32(sh_money["article_id"]) != article_id)
                return "红包失效"; //return "红包金额获取失败,未能添加红包";

            lock (lockObj)
            {
                try
                {
                    //获取会员信息
                    Dictionary<string, object> user = DaoUser.Get("user", "id=" + user_id, "shared_balance");

                    balance = DaoUser.BalanceDncode(user["shared_balance"], DES_KEY);
                    price = Convert.ToDecimal(sh_money["price"]);

                    //更新红包领取状态
                    count2 = DAOFCSharedMoney.UpdateState(money_id, 1);

                    //更新文章的剩余分享数
                    count3 = DAOFCArticle.Shared(article_id);

                    //更新分享的图片、价格信息
                    count4 = DAOFCShared.Shared(id, picurl, price);

                    //更新分享者余额
                    count1 = DaoUser.UpdateBalance(user_id, (balance + price), 2);
                }
                catch (Exception ex)
                {
                    isErr = true;
                    msg = ex.Message;
                    DaoLogcat.Add("shared_upload", string.Format("分享截图上传出错:{0},参数:id={1},user_id={2},money_id={3},picurl={4}", ex.Message, id, user_id, money_id, picurl));
                }

                if (isErr)
                {
                    //回滚 余额
                    if (count1 > 0)
                    {
                        decimal money = balance - price;
                        if (money < 0) money = 0;
                        DaoUser.UpdateBalance(user_id, money, 2);
                    }
                    //回滚 领取状态
                    if (count2 > 0)
                        DAOFCSharedMoney.UpdateState(money_id, -1);
                    //回滚 剩余分享数
                    if (count3 > 0)
                        DAOFCArticle.Shared(article_id, -1);

                    //回滚 图片、金额信息
                    if (count4 > 0)
                        DAOFCShared.Shared(id, "", 0);
                }
            }
            return msg;
        }
    }
}